package com.impetus.project.skilltest.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.impetus.project.skilltest.core.Constants;
import com.impetus.project.skilltest.core.SkillTestException;
import com.impetus.project.skilltest.dataobject.ScheduledTest;
import com.impetus.project.skilltest.util.HibernateUtil;

public class ScheduledTestDAO {

	private static Logger logger = Logger.getLogger(ScheduledTestDAO.class.getName());
	
	private static SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
	
	@SuppressWarnings("unchecked")
	public static List<ScheduledTest> getScheduledTestByCandidateId ( String candidateId )
	throws SkillTestException
	{
		List<ScheduledTest> scheduledTestList = new ArrayList<ScheduledTest>();
		
		try{
			
			scheduledTestList = sessionFactory.getCurrentSession()
			.createCriteria(ScheduledTest.class)
			.add(Restrictions.like("CANDIDATEID", candidateId))
			.add(Restrictions.like("STATUS", Constants.TEST_STATUS.SCHEDULED))
			.add(Restrictions.like("IS_ENABLED", "Y"))
			.list();
			
		}catch(Exception e)
		{
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}		
		
		return scheduledTestList;
	}
	
	@SuppressWarnings("unchecked")
	public static List<ScheduledTest> getScheduledTestByTestId ( String testId )
	throws SkillTestException
	{
		List<ScheduledTest> scheduledTestList = new ArrayList<ScheduledTest>();
		
		try
		{		
			scheduledTestList = sessionFactory.getCurrentSession()
			.createCriteria(ScheduledTest.class)
			.add(Restrictions.like("TESTID", testId))
			.list();
			
		}catch(Exception e)
		{
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}		
		
		return scheduledTestList;
	}
	
	public static void saveUpdateScheduledTest(ScheduledTest scheduledTest)
	throws SkillTestException
	{
		try
		{
			sessionFactory.getCurrentSession().saveOrUpdate(scheduledTest);
					
		}catch(Exception e)
		{
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}
	}	
	
	public static void deleteScheduledTest(ScheduledTest scheduledTest)
	throws SkillTestException
	{
		try
		{
			sessionFactory.getCurrentSession().delete(scheduledTest);
		}
		catch(Exception e)
		{
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}
	}
}
